<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Categories extends CI_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('category_model','category_model');
    }
    
    function index(){
        $this->listAll();
    }
    
    function listAll(){
        $menu['menu_1'] = '';
        $menu['menu_2'] = 'current';
        $menu['menu_3'] = '';
        $menu['menu_4'] = '';
        $menu['menu_5'] = '';
        
        $data['lists'] = $this->category_model->lists(0,1,500);
        
        $this->template->set_template('admin');
        $this->template->write_view('menu','menu_view',$menu);
        $this->template->write_view('content','categories_view',$data);
        $this->template->render();
    }
    
    function sub($id){
        $menu['menu_1'] = '';
        $menu['menu_2'] = 'current';
        $menu['menu_3'] = '';
        $menu['menu_4'] = '';
        $menu['menu_5'] = '';
        
        $data['main_category_name'] = $this->category_model->getCatNameByCatId($id);
        $data['lists'] = $this->category_model->lists($id,1,500);
        
        $this->template->set_template('admin');
        $this->template->write_view('menu','menu_view',$menu);
        $this->template->write_view('content','categories_sub_view',$data);
        $this->template->render();
    }
    
    function add(){
        $menu['menu_1'] = '';
        $menu['menu_2'] = 'current';
        $menu['menu_3'] = '';
        $menu['menu_4'] = '';
        $menu['menu_5'] = '';
        
        $data['parent_list'] = $this->category_model->lists(0,1,500);
        $data['message'] = $this->session->flashdata('message');
        $this->template->set_template('admin');
        $this->template->write_view('menu','menu_view',$menu);
        $this->template->write_view('content','categories_add_view',$data);
        $this->template->render();
    }
    
    function add_process(){
        if($this->session->userdata('admin_logged_in') == FALSE){
            redirect(base_url()."admin");
            exit();
        }
        
        if(isset($_POST['name_th']) AND $_POST['name_th']!='' AND isset($_POST['name_en']) AND $_POST['name_en']!=''){
            $input['parent'] = $_POST['parent'];
            $input['name_th'] = $_POST['name_th'];
            $input['name_en'] = $_POST['name_en'];
            $id = $this->category_model->add($input);
            
            if($id!=0){
                if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
                        $this->load->library('ciupload');
                        $this->ciupload->Upload($_FILES['img']);
                        if ($this->ciupload->uploaded){
                                $this->ciupload->image_resize = true;
                                $this->ciupload->image_ratio_fill = true;
                                $this->ciupload->image_x = 25;
                                $this->ciupload->image_y = 25;
                                $this->ciupload->Process(APPPATH.'../assets/image/category/'.$id);
                                
                                if ($this->ciupload->processed){
                                        $input['id'] = $id;
                                        $input['img'] = $this->ciupload->file_dst_name;
                                        $result = $this->category_model->update_img($input);
                                        if($result==0){
                                                @unlink(APPPATH.'../assets/image/category/'.$id.'/'.$this->ciupload->file_dst_name);
                                                $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
                                        }else{
                                                redirect(base_url()."admin/categories");
                                        }
                                }else{
                                        $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
                                        redirect(base_url()."admin/categories");
                                }
                        }else{
                                $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
                                redirect(base_url()."admin/categories");
                        }
                }else{
                        $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
                        redirect(base_url()."admin/categories");
                }
            }else{
                $this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
                redirect(base_url()."admin/category/add");
            }
        }else{
            $this->session->set_flashdata('message', 'กรุณากรอก Name (th) และ Name (en)');
            redirect(base_url()."admin/categories/add");
        }
    }
    
    function edit($id){
        $menu['menu_1'] = '';
        $menu['menu_2'] = 'current';
        $menu['menu_3'] = '';
        $menu['menu_4'] = '';
        $menu['menu_5'] = '';
        
        $data['category_info'] = $this->category_model->getCatInfoByCatId($id);
        $data['parent_list'] = $this->category_model->lists(0,1,500);
        $data['message'] = $this->session->flashdata('message');
        
        $this->template->set_template('admin');
        $this->template->write_view('menu','menu_view',$menu);
        $this->template->write_view('content','categories_edit_view',$data);
        $this->template->render();
    }
    
    function edit_process(){
        if($this->session->userdata('admin_logged_in') == FALSE){
            redirect(base_url()."admin");
            exit();
        }
        
        if(isset($_POST['id']) AND isset($_POST['name_th']) AND $_POST['name_th']!='' AND isset($_POST['name_en']) AND $_POST['name_en']!=''){
            $id = $_POST['id'];
	    $input['id'] = $id;
            $input['parent'] = $_POST['parent'];
            $input['name_th'] = $_POST['name_th'];
            $input['name_en'] = $_POST['name_en'];
            $check = $this->category_model->update($input);
            
            if($check){
                if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
                    $this->load->library('ciupload');
                    $this->ciupload->Upload($_FILES['img']);
                    if ($this->ciupload->uploaded){
                        $this->ciupload->image_resize = true;
                        $this->ciupload->image_ratio_fill = true;
                        $this->ciupload->image_x = 25;
                        $this->ciupload->image_y = 25;
                        $this->ciupload->Process(APPPATH.'../assets/image/category/'.$id);
                        
                        if ($this->ciupload->processed){
                            $input['id'] = $id;
                            $input['img'] = $this->ciupload->file_dst_name;
                            $old_img = $this->category_model->get_img($input);
                            $result = $this->category_model->update_img($input);
                            if($result==0){
                                @unlink(APPPATH.'../assets/image/category/'.$id.'/'.$this->ciupload->file_dst_name);
                                $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');;
redirect(base_url()."admin/category/edit/{$id}");
                        }else{
                                if($old_img->img!='' AND file_exists(APPPATH.'../assets/image/category/'.$id.'/'.$old_img->img)){
                                    @unlink(APPPATH.'../assets/image/category/'.$id.'/'.$old_img->img);
                                }
                                redirect(base_url()."admin/categories");
                            }
                        }else{
                            $this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
                            redirect(base_url()."admin/categories/edit/{$id}");
                        }
                    }
                }else{
                    redirect(base_url()."admin/categories");
                }
            }else{
                $this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
                redirect(base_url()."admin/categories/edit/{$id}");
            }
        }else{
            $this->session->set_flashdata('message', 'กรุณากรอก Name (th) และ Name (en)');
            redirect(base_url()."admin/categories");
        }
    }
    
    public function del($id)
    {
        if($this->session->userdata('admin_logged_in') == FALSE){
            redirect(base_url()."admin");
        }
        
        if(($id+0)>0){
            $this->category_model->delete($id+0);
            if($this->category_model->delete($id+0)){
                $this->load->helper('devlism_helper');
                remove_dir(APPPATH.'../assets/image/category/'.$id);
            }
            redirect(base_url()."admin/categories");
        }else{
            redirect(base_url()."admin/categories");
        }
    }
}

/* End of file categories.php */
/* Location: ./application/controllers/admin/categories.php */